use thiserror::Error;
use serde::Serialize;

#[derive(Error, Debug, Serialize)]
pub enum AppError {
    #[error("认证失败: {0}")]
    AuthError(String),
    
    #[error("权限不足: {0}")]
    PermissionError(String),
    
    #[error("参数错误: {0}")]
    ValidationError(String),
    
    #[error("资源不存在: {0}")]
    NotFoundError(String),
    
    #[error("操作失败: {0}")]
    OperationError(String),
    
    #[error("SIP 错误: {0}")]
    SipError(String),
    
    #[error("系统错误: {0}")]
    SystemError(String),
    
    #[error("未知错误: {0}")]
    UnknownError(String),
}

pub type AppResult<T> = Result<T, AppError>;

impl From<serde_json::Error> for AppError {
    fn from(err: serde_json::Error) -> Self {
        AppError::ValidationError(format!("JSON 解析错误: {}", err))
    }
}

impl From<std::io::Error> for AppError {
    fn from(err: std::io::Error) -> Self {
        AppError::SystemError(format!("IO 错误: {}", err))
    }
}
